package creditsProject.server.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import creditsProject.dto.Credit;
import creditsProject.dto.RequestDto;
import creditsProject.dto.User;
import creditsProject.server.dao.CreditDao;

public class CreditDaoImpl implements CreditDao {

	final String derbyProtocol = "jdbc:derby://localhost:1527/";
	final String dbName = "BU";
	final String jdbcURL = derbyProtocol + dbName;
	final String derbyDriver = "org.apache.derby.jdbc.ClientDriver";

	@Override
	public Credit getCreditById(int id) {

		Connection conn = null;
		try {
			System.setProperty("jdbc.drivers", derbyDriver);
			conn = DriverManager.getConnection(jdbcURL, "admin", "admin");

			PreparedStatement ps = conn
					.prepareStatement("select CREDIT_ID, NAME, DESCRIPTION, DURATION from APP.CREDITS WHERE CREDIT_ID=?");
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Credit credit = new Credit();
				credit.setCreditId(rs.getInt("CREDIT_ID"));
				credit.setName(rs.getString("NAME"));
				credit.setDescription(rs.getString("DESCRIPTION"));
				credit.setDuration(rs.getString("DURATION"));
				return credit;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;

	}

	@Override
	public List<Credit> getAllCredits() {
		List<Credit> res = new ArrayList<Credit>();

		Connection conn = null;
		try {
			System.setProperty("jdbc.drivers", derbyDriver);
			conn = DriverManager.getConnection(jdbcURL, "admin", "admin");

			PreparedStatement ps = conn
					.prepareStatement("select CREDIT_ID, NAME, DESCRIPTION, DURATION from APP.CREDITS");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Credit credit = new Credit();

				credit.setCreditId(rs.getInt("CREDIT_ID"));
				credit.setName(rs.getString("NAME"));
				credit.setDescription(rs.getString("DESCRIPTION"));
				credit.setDuration(rs.getString("DURATION"));
				res.add(credit);
			}
			return res;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;

	}

	@Override
	public void addCredit(Credit credit) {
		Connection conn = null;
		System.setProperty("jdbc.drivers", derbyDriver);
		try {
			conn = DriverManager.getConnection(jdbcURL, "admin", "admin");
			PreparedStatement ps = conn
					.prepareStatement("select CREDIT_ID from app.CREDITS ORDER BY CREDIT_ID DESC");
			ResultSet rs = ps.executeQuery();
			List<Integer> previuosIds = new ArrayList<>();
			while(rs.next()){
				previuosIds.add(rs.getInt("CREDIT_ID"));
				}
				Integer previuosId = previuosIds.get(0);
			ps = conn
					.prepareStatement("insert into app.CREDITS (CREDIT_ID, NAME, DESCRIPTION, DURATION) values (?,?,?,?)");
			ps.setInt(1, previuosId + 1);
			ps.setString(2, credit.getName());
			ps.setString(3, credit.getDescription());
			ps.setString(4, credit.getDuration());
			ps.executeUpdate();
			System.out.println("Credit created" + credit.getName());

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}